import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.*;
import java.awt.event.ItemEvent;

public class Menu  extends JFrame  implements ActionListener,ItemListener {
    JMenuItem jMenuNew = new JMenuItem("新建");
    JMenuItem jMenuOpen= new JMenuItem("打开");
    JMenuItem jMenuExit = new JMenuItem("退出");
    //单选按钮菜单项
    JRadioButtonMenuItem  blue = new JRadioButtonMenuItem("蓝色");
    JRadioButtonMenuItem  green = new JRadioButtonMenuItem("绿色");



    public Menu() {
        super("记事本");
        this.setSize(600,400);
        //1.创建菜单栏，设置到框架上
        JMenuBar jmb = new JMenuBar();
        this.setJMenuBar(jmb);
        //2.创建菜单，加到菜单栏上
        JMenu file = new JMenu("文件");
        JMenu option = new JMenu("选项");
        jmb.add(option);
        jmb.add(file);
        //3.创建菜单项，加到菜单上
        file.add(jMenuNew);
        file.add(jMenuOpen);
        file.addSeparator();//加分割线
        file.add(jMenuExit);

        jMenuNew.addActionListener(this);
        jMenuOpen.addActionListener(this);
        jMenuExit.addActionListener(this);


        JMenu color = new JMenu("颜色");
        option.add(color);
        color.add(blue);
        color.add(green);
        ButtonGroup bt = new ButtonGroup();
        bt.add(blue);
        bt.add(green);

        blue.addItemListener(this);
      green.addItemListener(this);


        this.setVisible(true);
        this.setLocationRelativeTo(null);//**在中间跳出
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new Menu();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==jMenuNew)
            JOptionPane.showMessageDialog(getParent(),"新建文件");
        if(e.getSource()==jMenuOpen)
            JOptionPane.showMessageDialog(getParent(),"打开文件");


        if(e.getSource()==jMenuExit)
            System.exit(0);

    }
    public void itemStateChanged(ItemEvent e) {
        if(e.getSource()==blue)
            this.getContentPane().setBackground(Color.blue);
        if(e.getSource()==green)
            this.getContentPane().setBackground(Color.green);
    }
}
